import sys

from lib.constant.lib_constant import LibConstant
from lib.handler.redis_handler import RedisHandler
from lib.handler.verify_handler import VerifyHandler
from lib.init.scheduler_init import SchedulerInit
from lib.property.application_property import ApplicationProperty
from lib.sys.redis_factory import RedisFactory
from lib.utils.file_util import FileUtil
from lib.utils.print_util import PrintUtil


class ApplicationInit(object):
    prop = None

    @staticmethod
    def set_prop(prop):
        ApplicationInit.prop = prop

    @staticmethod
    def start(server_name):

        PrintUtil.start("Start application setting ...")
        # 处理redis
        redis_host = ApplicationInit.prop.get(ApplicationProperty.SYSTEM_REDIS_HOST)
        redis_port = ApplicationInit.prop.get(ApplicationProperty.SYSTEM_REDIS_PORT)
        redis_db = ApplicationInit.prop.get(ApplicationProperty.SYSTEM_REDIS_DB)
        redis_password = ApplicationInit.prop.get(ApplicationProperty.SYSTEM_REDIS_PW)

        PrintUtil.start("init redis setting start ...")
        if redis_host and redis_port:
            if not redis_db:
                redis_db = 0
            RedisHandler.handler(redis_host, int(redis_port), redis_db, redis_password)
            RedisFactory.init()
            PrintUtil.start("init redis success ,the host is [%s], the port is [%s] " % (redis_host, redis_port))
        else:
            PrintUtil.start("Start redis server fail, because it is can not find [host, port] ...")
            sys.exit(0)

        PrintUtil.start("init verify code start ...")
        # 打码平台
        verify_account = ApplicationInit.prop.get(ApplicationProperty.SYSTEM_VERIFY_USERNAME)
        verify_pw = ApplicationInit.prop.get(ApplicationProperty.SYSTEM_VERIFY_PW)
        verify_id = ApplicationInit.prop.get(ApplicationProperty.SYSTEM_VERIFY_ID)
        if verify_account and verify_account and verify_id:
            VerifyHandler.handler(verify_account, verify_pw, verify_id)
            PrintUtil.start("init verify code success ,the account is [%s] " % verify_account)
        else:
            PrintUtil.start("cannot use verify code,  because it is can not find  [username, password, id]  ...")
            if server_name == LibConstant.SERVER_LOGIN:
                sys.exit(0)

        # 图片保存路径,如果不存在自动生成
        FileUtil.exists_folder(ApplicationInit.prop.get(ApplicationProperty.SYSTEM_P_SCREEN_ADDRESS))

        # 驱动验证
        driver_number = 0
        chrome_driver = ApplicationInit.prop.get(ApplicationProperty.SYSTEM_DRIVER_CHROME_ADDRESS)
        if FileUtil.exists_file(chrome_driver):
            driver_number += 1
        else:
            PrintUtil.start("cannot load chrome driver,  because it is can not find [%s] ..." % chrome_driver)

        firebox_driver = ApplicationInit.prop.get(ApplicationProperty.SYSTEM_DRIVER_FIREBOX_ADDRESS)
        if FileUtil.exists_file(firebox_driver):
            driver_number += 1
        else:
            PrintUtil.start("cannot load firebox driver,  because it is can not find [%s] ..." % chrome_driver)

        if driver_number == 0 and server_name == LibConstant.SERVER_LOGIN:
            PrintUtil.start("start login server error ...")
            sys.exit(0)

        # 调度服务配置
        SchedulerInit.start()
